
function func() {

}

//定义用户
function User(o){
    if(null!==o){
    this.created_at = o.created_at;
    this.last_device = o.last_device;
    this.role =o.role;
    this.last_visited_at = o.last_visited_at;
    this.state = o.state;
    this.login = o.login;
    this.icon = o.icon;}
    else{
        return null;
    }
}

//
function Votes(o){
    this.down = o.down;
    this.up = o.up;
}

function Qiushi(o){
    if(null!==o&&undefined!==o){
    if(null!==o.user&&undefined!==o.user){
        this.usercreated_at = o.user.created_at;
        this.userlast_device = o.user.last_device;
        this.userrole =o.user.role;
        this.userlast_visited_at = o.user.last_visited_at;
        this.userstate = o.user.state;
        this.userlogin = o.user.login;
        this.userid = o.user.id;
        this.usericon = o.user.icon;


    }
    else{
        this.userlogin = "匿名投递";
        this.usericon = null;
        this.userid = null;
    }

    this.image = o.image;
    this.published_at = o.published_at;
    this.tag = o.tag;
    this.id = o.id;
    if(null!==o.votes){
        this.down = o.votes.down;
        this.up =o.votes.up;
    }
    else{
        this.down = 0;
        this.up = 0;
    }

    this.created_at = o.created_at;
    this.content = o.content;
    this.state = o.state;
    this.comments_count = o.comments_count;
    this.allow_comment = o.allow_comment;
   }
    else{
    return null
    }

}
//初始化
function init()
{
 try{
appWindow.pageStack.pop(mainPage);}
 catch(e){
    // console.log("pop error: " +e)
}
}
 var xhr = new XMLHttpRequest;
function load(urls){
    qiushi.visible = false
    indicator1.visible = true;
    var URL = "";
    if(null===urls||urls.length<=1){

     URL = nowurl;
    console.log("urls is null"+urls);
    }
    else{
        console.log("url is not null ")
        URL = urls
    }
           xhr.abort()
     xhr.open("GET", URL);
     xhr.setRequestHeader("source","android_1.1")
    if (appWindow.qbtoken !== "" )
        xhr.setRequestHeader("qbtoken", appWindow.qbtoken)

           view.loadMoreFlag = false;
           xhr.onreadystatechange = function() {
               if (xhr.readyState === XMLHttpRequest.DONE)
               {
                   var jj = xhr.responseText;
                  // console.log("====="+jj);
                   if(null!==jj&&undefined!==jj&&""!=jj)
                    {
                       var a = JSON.parse(jj);
                       view.sumcount = a.total;
                       for (var i in a.items){

                           listModel.append(a.items[i])

                       }

               indicator1.visible = false;
                qiushi.visible = false

                   }
                   else if(jj.indexOf('total": 0,')>-1) {
                   console.log("==============="+xhr.responseText);
                   indicator1.visible = false;
                   qiushitext.text ="穿越失败";
                  // loadmore.enabled =false;
                   qiushi.visible=true;
                   }

                   else{
                       console.log("==============="+xhr.responseText);
                       indicator1.visible = false;
                       qiushitext.text ="服务器去月球了？还是你么有联网?检查下链接吧";
                      // loadmore.enabled =false;
                       qiushi.visible=true;
                   }

                }

           }
           xhr.send();

}

var count = 1

function callbackFunction() {
    var s = numbermodel.get(dayColumn.selectedIndex).value +
            numbermodel.get(monthColumn.selectedIndex).value *10 +
            numbermodel.get(yearColumn.selectedIndex).value*100;
    //Web.showMessage(s);
    if(s*30< view.sumcount)
    gotowhere(s);
    else
        Web.showMessage("你想去的地方不存在");

}
function gotowhere(wheretogo){
     listModel.clear();
    count = wheretogo-1;
    console.log("where go to is"+count);
    getmore();
}

function getmore(){

    //loadmore.enabled = false;

    count =count+1;
  //  pagecount.text = count+"";
    view.loadMoreFlag = true;

    var URL = nowurl;
    console.log("count now is"+count+" -----url is"+URL);
    var lastequile = URL.lastIndexOf("=");
    URL = URL.substring(0,lastequile+1)+count;
    console.log("url is url  getmore()  "+URL);
    load(URL);
    //loadmore.enabled =true;
}
function resfer(){

    //loadmore.enabled = false;

    count =1;
    //pagecount.text = count+"";


    var URL = nowurl;
    URL = URL.replace("pagecount",count);
    //console.log("url is url "+URL);
    load(URL);
    //loadmore.enabled =true;
}

function loadmine(){
    count =0;
    nowurl = myarticle
    getmore()
}

function changecount(which){
    count =0;

    switch(which){
    case 0: nowurl =lasteurl;
        break;
      case 1:nowurl = imageurl;
          break;
      case 2 : nowurl = suggesturl;
          break;
      case 3:nowurl = dayurl;
          break;
      case 4:nowurl = weekurl;
          break;
      case 5:nowurl = monthurl;
          break;
      case 6:

          nowurl = history;
         nowurl= nowurl.replace("%1",getRandomDate());
          console.log("now url is " +nowurl);
          break;
      default:nowurl = lasteurl
          break;
    }
    getmore()
}




var nowurl = "http://m2.qiushibaike.com/article/list/latest?count=30&page=1";
        //define LastestURLString(count,page) [NSString stringWithFormat:@
 var lasteurl = "http://m2.qiushibaike.com/article/list/latest?count=30&page=pagecount";
        //define ImageURLString(count,page) [NSString stringWithFormat:@
 var imageurl = "http://m2.qiushibaike.com/article/list/images?count=30&page=pagecount";
        //define SuggestURLString(count,page) [NSString stringWithFormat:@
 var suggesturl = "http://m2.qiushibaike.com/article/list/suggest?count=30&page=pagecount";
        //define DayURLString(count,page) [NSString stringWithFormat:@
 var dayurl = "http://m2.qiushibaike.com/article/list/day?count=30&page=pagecount";
        //define WeakURlString(count,page) [NSString stringWithFormat:@
var weekurl = "http://m2.qiushibaike.com/article/list/week?count=30&page=pagecount";
        //define MonthURLString(count,page) [NSString stringWithFormat:@
var monthurl = "http://m2.qiushibaike.com/article/list/month?count=30&page=pagecount";
       // #define CommentsURLString(ID) [NSString stringWithFormat:@
var coments = "http://m2.qiushibaike.com/article/%@/comments?count=30&page=pagecount";

var history = "http://m2.qiushibaike.com/article/history/%1?count=30&page=pagecount"

var myarticle = "http://m2.qiushibaike.com/user/my/articles?page=pagecount&count=30"

function getRandomDate(){
    var ori = new Date(2007,1,1)
    return Qt.formatDate(new Date(ori.getTime()+Math.round(Math.random()*(new Date()-ori))), "yyyy-MM-dd")
}
